﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using WooCoo.ObjectModel;
using WooCoo.Portal.Contracts;
using WooCoo.Security;

namespace WooCoo.Portal.Service
{
    /// <summary>
    /// 系统登陆相关服务
    /// </summary>
    [Export("WooCoo.Portal.Contracts.ILoginService", typeof(ILoginService))]
    public class LoginService : ILoginService
    {
        public LoginInfo Login(string usercode, string password)
        {
            var user = RepositoryFactoryHost.Factory.GetRepository<SysUser>().AsQueryable().Where(u => u.UserCode == usercode).FirstOrDefault();
            //该用户不存在
            if (user == null || string.IsNullOrEmpty(user.UserCode))
            {
                return new LoginInfo() { LoginResult = LoginResult.AccountNonExist };
            }
            //用户已禁用
            else if (user.State == "0")
            {
                return new LoginInfo() { LoginResult = LoginResult.AccountDisable };
            }
            return new LoginInfo() { LoginResult = LoginResult.Success, SessionID = user.UserCode };
        }


        public UserPrincipal GetUserPrincipal(string sessionID)
        {
            UserIdentity uid = new UserIdentity();
            uid.UserCode = "1";
            uid.UserName = "Authed";
            UserPrincipal up = new UserPrincipal(uid);
            return up;
        }
    }
}
